<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Model\HillclimbUsers;
use App\Model\HillclimbInvite;
use EasyWeChat\Foundation\Application;

class AuthController extends Controller
{
    protected $_app;
    public function __construct(Request $request){
        $options = [
            'use_laravel_cache' => true,
            'debug'     => true,
            'app_id'    => 'wx5f4b5d0b03bd9e90',
            'secret'    => 'ef3a73540e9697e5930bdee5d3991849',
            'token'     => 'laravelwechat',
            'log' => [
                'level' => 'debug',
                'file'  => storage_path('logs/wechat.log'),
            ],
            // ...
        ];
        $app = new Application($options);
        $this->_app = $app;
    }
    public function base(){
        $response = $this->_app->oauth->scopes(['snsapi_userinfo'])->redirect(url(env('URL_SUB', '').'/ds/auth/callback'));
        return $response;
    }
    public function hillclimb(Request $request){
        $oauth = $this->_app->oauth;
        // 获取 OAuth 授权结果用户信息
        $user = $oauth->user();
        $user = $user->toArray();
        $original = $user['original'];
        $_SESSION['openid'] = $openid = $user['id'];
        $userdata = HillclimbUsers::where("openid", $user['id'])->first();
        if (!empty($userdata)) {
            $id=$userdata->id;
            setcookie("openid",$openid,time()+315360000,'/');
            setcookie("id",$userdata->id,time()+315360000,'/');
            if(isset($_SESSION["inviteid"]) && $_SESSION["inviteid"] != ''){
                $invites = HillclimbInvite::where(['from_uid'=>$_SESSION["inviteid"],'to_uid'=>$id])->first();
                if(count($invites) <= 0){
                    $joininvite = new HillclimbInvite();
                    $joininvite->from_uid = $_SESSION["inviteid"];
                    $joininvite->to_uid = $id;
                    $joininvite->save();
                }
            }
        } else {
            $id = HillclimbUsers::insertGetId([
                'openid'=>$openid,
                'nickname'=>$user['nickname'],
                'gender'=>  isset($original['sex'])?$original['sex']:'0',
                'province'=>  isset($original['province'])?$original['province']:'',
                'city'=>  isset($original['city'])?$original['city']:'',
                'country'=>  isset($original['country'])?$original['country']:'',
                'avatar'=>  isset($original['headimgurl'])?$original['headimgurl']:''
            ]);
            $invite = new HillclimbInvite();
            $invite->from_uid = $id;
            $invite->to_uid = $id;
            $invite->save();
            if(isset($_SESSION["inviteid"]) && $_SESSION["inviteid"] != ''){
                $invites = HillclimbInvite::where(['from_uid'=>$_SESSION["inviteid"],'to_uid'=>$id])->first();
                if(count($invites) <= 0){
                    $joininvite = new HillclimbInvite();
                    $joininvite->from_uid = $_SESSION["inviteid"];
                    $joininvite->to_uid = $id;
                    $joininvite->save();
                }
            }
            setcookie("openid",$openid,time()+315360000,'/');
            setcookie("id",$id,time()+315360000,'/');
        }
        setcookie("is_base",'1',time()+315360000,'/');
        return redirect($_SESSION['target_url']); // 跳转到 user/profile
    }
    
}